package com.shwread.android.qysw.activity.task;

import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.GlideUtil;
import com.NewActionHelp;
import com.ciko.code.base.BaseActicity;
import com.google.gson.reflect.TypeToken;
import com.image_see.ImageSeeActivity;
import com.shwread.android.qysw.adapter.TaskAdapter;
import com.shwread.android.qysw.bean.task.UserActivityBean;
import com.shwread.android.qysw10000241.R;
import com.shwread.http.base.ObjectHttpResponseHandler;

import net.nightwhistler.htmlspanner.TextUtil;

import org.apache.http.Header;

import java.lang.reflect.Type;
import java.util.ArrayList;

import logic.util.Utils;

/**
 * 个人活动任务详情
 */
public class PersonalTaskDetailActivity extends BaseActicity {
    /**
     * 监督状态
     */
    private ImageView imgSuperviseStatus;
    /**
     * 活动名称
     */
    private TextView tvTaskName;
    /**
     * 分项
     */
    private TextView tvTaskSubItem;
    /**
     * 类别
     */
    private TextView tvTaskCategory;
    /**
     * 指数类型
     */
    private TextView tvTaskType;
    /**
     * 活动分值
     */
    private TextView tvTaskValue;
    /**
     * 申请时间
     */
    private TextView tvAskTaskTime;
    /**
     * 凭证
     */
    private ImageView imgTaskTestimony;
    /**
     * 党委监督结果item
     */
    private LinearLayout llSuperviseStatus;
    /**
     * 党委监督结果
     */
    private TextView tvSuperviseStatus;
    /**
     * 党委监督时间item
     */
    private LinearLayout llSuperviseTime;
    /**
     * 党委监督时间
     */
    private TextView tvTaskSuperviseTime;
    /**
     * 数据对象
     */
    private UserActivityBean taskBean;

    @Override
    protected void initTopBar() {
        setTitle("个人活动任务");
        enableNavLeftImg();
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_personal_task_detail);
    }

    @Override
    protected void initView() {
        imgSuperviseStatus = findViewByID(R.id.img_supervise_status);
        tvTaskName = findViewByID(R.id.tv_task_name);
        tvTaskSubItem = findViewByID(R.id.tv_task_sub_item);
        tvTaskCategory = findViewByID(R.id.tv_task_category);
        tvTaskType = findViewByID(R.id.tv_task_type);
        tvTaskValue = findViewByID(R.id.tv_task_value);
        tvAskTaskTime = findViewByID(R.id.tv_ask_task_time);
        imgTaskTestimony = findViewByID(R.id.img_task_testimony);
        llSuperviseStatus = findViewByID(R.id.ll_supervise_status);
        tvSuperviseStatus = findViewByID(R.id.tv_supervise_status);
        llSuperviseTime = findViewByID(R.id.ll_supervise_time);
        tvTaskSuperviseTime = findViewByID(R.id.tv_task_supervise_time);
    }

    @Override
    protected void initAdapter() {

    }

    @Override
    protected void initDates() {
        int taskId = getIntent().getIntExtra("taskId", 0);
        NewActionHelp.getPersonActivityInfo(this, taskId, new ObjectHttpResponseHandler<UserActivityBean>() {
            @Override
            public void onFailure(int i, Header[] headers, String s, Throwable throwable) {
                Log.e("sss", "失败--" + s);
                Utils.showToast(PersonalTaskDetailActivity.this, "获取详情信息失败");
            }

            @Override
            public void onSuccess(int i, Header[] headers, UserActivityBean userActivityBean, String s) {
                Log.e("sss", "成功--" + s);
                taskBean = userActivityBean;
                setView();
            }

            @Override
            public Type getType() {
                return new TypeToken<UserActivityBean>() {
                }.getType();
            }
        });
    }

    private void setView() {
        if (taskBean == null) {
            return;
        }
        tvTaskName.setText(taskBean.getActivityName());
        tvTaskSubItem.setText(taskBean.getIndexName());
        tvTaskCategory.setText(taskBean.getKindName());
        tvTaskType.setText(taskBean.getIndex_type());
        tvTaskValue.setText(String.format("%s分", Utils.getScore(taskBean.getScore())));
        tvAskTaskTime.setText(TaskAdapter.getTime(taskBean.getApplyTime()));
        switch (taskBean.getActivityStatus()) {
            case "1":
                tvSuperviseStatus.setText("监督中");
                imgSuperviseStatus.setImageResource(R.drawable.supervision);
                llSuperviseStatus.setVisibility(View.GONE);
                llSuperviseTime.setVisibility(View.GONE);
                break;
            case "2":
                tvSuperviseStatus.setText("监督通过");
                imgSuperviseStatus.setImageResource(R.drawable.supervision_pass);
                llSuperviseStatus.setVisibility(View.VISIBLE);
                llSuperviseTime.setVisibility(View.VISIBLE);
                break;
            case "3":
                if (!TextUtils.isEmpty(taskBean.getPassReason())) {
                    tvSuperviseStatus.setText(taskBean.getPassReason());
                }
                imgSuperviseStatus.setImageResource(R.drawable.supervision_not_pass);
                llSuperviseStatus.setVisibility(View.VISIBLE);
                llSuperviseTime.setVisibility(View.VISIBLE);
                break;
        }
        tvTaskSuperviseTime.setText(TaskAdapter.getTime(taskBean.getControlTime()));
        GlideUtil.load(this, taskBean.getVoucherData(), imgTaskTestimony);
    }

    @Override
    protected void initListener() {
        imgTaskTestimony.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.img_task_testimony://凭证
                Intent intent = new Intent(this, ImageSeeActivity.class);
                ArrayList<String> urlList = new ArrayList<>();
                urlList.add(taskBean.getVoucherData());
                intent.putStringArrayListExtra("urlList", urlList);
                startActivity(intent);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        NewActionHelp.cancel(this);
    }
}
